<?php
namespace Classes;
use Illuminate\Support\Facades\DB;
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/12
 * Time: 15:05
 */
class User
{
    public $user_id;
    public function __construct($userId)
    {
        $this->user_id = $userId;
    }

    public static function selectSimple($userId){
        $users = DB::table('weibo_users')->where('user_id','=',$userId)->select('user_id','user_nickname','user_level','user_head_img')->get();
        return $users[0];
    }

    public static function login($email,$password){
        $users = DB::table("weibo_users");
        $hash_password = hash('md5',$password);
        $result = $users->where('user_email','=',$email)->where('user_password','=',$hash_password)->select('user_id')->get();
        if ($result){
            $token = hash('md5',Common::getRand());
            $users->where('user_id','=',$result[0]->user_id)->update(['user_token' => $token]);
            setcookie('user_token',$token,time()+3600*24*7,'/');
            setcookie('user_id',$result[0]->user_id,time()+3600*24*7,'/');
            return $token;
        }else{
            setcookie("user_token", "", time()-3600,'/');
            setcookie('user_id','',time()-3600,'/');
            return false;
        }
    }

    public static function insert($dataArray){
        $data = [];
        $users = DB::table('weibo_users');
        $data['user_nickname'] = $dataArray['user_nickname'];
        $data['user_email'] = $dataArray['user_email'];
        $data['user_password'] = hash('md5',$dataArray['user_password']);
        $data['user_token'] = hash('md5',User::getRand().$data['user_email']);
        $result = $users->where('user_token','=', $data['user_token'])->count();
        if($result){
            User::insert($dataArray);
        }
        else{
            $users = $users->insertGetId($data);
            if($users){
                setcookie('user_token',$data['user_token'],time()+3600*24*7,'/');
                setcookie('user_id',$users,time()-3600,'/');
                return $users;
            }
            else{
                setcookie("user_token", "", time()-3600,'/');
                setcookie('user_id','',time()-3600,'/');
                return false;
            }
        }
        return false;
    }

    public static function passwordVerifiy($email,$password){
        $users = DB::table("weibo_users");
        $hash_password = hash('md5',$password);
        $token = null;
        $result = $users->where('user_email','=',$email)->where('user_password','=',$hash_password)->select('user_id')->get();
        if ($result){
            $token = hash('md5',Common::getRand());
            $users->where('user_id','=',$result[0]->user_id)->update(['user_token' => $token]);
            setcookie('user_token',$token,time()+3600*24*7,'/');
            setcookie('user_id',$result[0]->user_id,time()+3600*24*7,'/');
        }else{
            setcookie("user_token", "", time()-3600,'/');
            setcookie('user_id','',time()-3600,'/');
        }
        return $token;
    }

    public static function updatePassword($email,$newPassword){
        $newPassword = hash('md5',$newPassword.$email);
        $user = DB::table('weibo_users');
        $result = $user->where('user_email','=',$email)->update(['user_password' => $newPassword]);
        if($result){
            return true;
        }
        else{
            return false;
        }
    }

    public static function tokenVerifiy($token){
        $users = DB::table('weibo_users');
        $result = $users->where('user_token', '=', $token)->select('user_id','user_nickname','user_head_img','user_card_img','user_sex','user_bowen_count','user_followed_count','user_following_count','user_level')->get();
        return $result;
    }

    public static function isEmail($email){
        $pattern = "/^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/";
        if(preg_match($pattern,$email)){
            return true;
        }
        else{
            return false;
        }
    }

    public static function isPassword($password){
        $pattern = "/\w{6,18}$/";
        if(preg_match($pattern,$password)){
            return true;
        }
        else{
            return false;
        }
    }

    public static function isNickName($nickname){
        $pattern = "/[@#\$%\^&\*]+/";
        if (preg_match($pattern,$nickname)){
            return false;
        }
        else{
            return true;
        }
    }

    public static function isNicknameExist($nickname){
        $users = DB::table('weibo_users');
        $users = $users->where('user_nickname','=',$nickname)->count();
        if($users){
            return true;
        }
        else{
            return false;
        }
    }

    public static function isEmailExist($email){
        $users = DB::table('weibo_users');
        $users = $users->where('user_email', '=', $email)->count();
        if($users){
            return true;
        }
        else{
            return false;
        }
    }
}